﻿using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    /// <summary>
    /// 视频数据服务类
    /// </summary>
    public class VideoService : IVideoService
    {
        /// <summary>
        /// 待上传视频
        /// </summary>
        /// <param name="videoId">视频Id</param>
        /// <param name="userId">用户Id</param>
        /// <param name="headline">标题</param>
        /// <param name="location">位置</param>
        /// <returns></returns>
        public TBVideos ToBeUpload(string videoId, int userId, string headline, string location = null)
        {
            if (string.IsNullOrWhiteSpace(videoId))
            {
                throw new ArgumentNullException(nameof(videoId));
            }
            if (string.IsNullOrWhiteSpace(headline))
            {
                throw new ArgumentNullException(nameof(headline));
            }
            TBVideos videoEntity = new TBVideos()
            {
                VideoId = videoId,
                UserId = userId,
                Headline = headline,
                Location = location,
                Status = (int)VideoStatus.Uploading,
                UploadTime = DateTime.Now
            };
            AiXiuDbContext dbContext = new AiXiuDbContext();
            dbContext.Entry(videoEntity).State = EntityState.Added;
            dbContext.SaveChanges();
            return videoEntity;
        }

        /// <summary>
        /// 获取处理中的视频Id列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetInProcessVideoIds()
        {
            List<int> queryStatus = new List<int>()
            {
                (int)VideoStatus.Uploading,         // 上传中的视频
                (int)VideoStatus.UploadSucces,      // 上传成功的视频
                (int)VideoStatus.Transcoding        // 转码中的视频
            };
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBVideos.Where(m => queryStatus.Contains(m.Status)).Select(m => m.VideoId).ToList();
        }

        /// <summary>
        /// 批量更新视频信息
        /// </summary>
        /// <param name="videos">要更新的视频列表</param>
        /// <returns></returns>
        public async Task UpdateVideos(List<TBVideos> videos)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBVideos videoEntity;
            foreach (TBVideos video in videos)
            {
                videoEntity = dbContext.TBVideos.SingleOrDefault(m => m.VideoId == video.VideoId);
                if (videoEntity != null)
                {
                    videoEntity.CoverURL = video.CoverURL.Substring(0, video.CoverURL.IndexOf("?"));
                    videoEntity.Status = video.Status;
                }
                dbContext.Entry(videoEntity).State = EntityState.Modified;
                await dbContext.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            int statusValue = (int)VideoStatus.Normal;
            return dbContext.TBVideos       // 查询视频信息表
                .Where(m => m.Status == statusValue)
                .Include(m => m.TBUsers)    // 连接查询用户信息表
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }

        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBVideos video = dbContext.TBVideos
                .Include(m => m.TBUsers)
                .SingleOrDefault(m => m.VideoId == videoId);
            return video;
        }

        /// <summary>
        /// 通过用户Id获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            return dbContext.TBVideos
                .Where(m => m.UserId == userId)
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }

        /// <summary>
        /// 删除视频
        /// </summary>
        /// <param name="videoId">视频Id</param>
        public void DeleteVideo(string videoId)
        {
            if (string.IsNullOrWhiteSpace(videoId))
            {
                throw new ArgumentNullException(nameof(videoId));
            }
            // 从数据库删除
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBVideos videoEntity = dbContext.TBVideos
                .SingleOrDefault(m => m.VideoId == videoId);
            if (videoEntity != null)
            {
                dbContext.Entry(videoEntity).State = EntityState.Deleted;
                dbContext.SaveChanges();
            }
        }
    }
}